﻿using System;
using System.Timers;
using SVU.Model;

namespace SVU.Managers
{
    public class AlarmManager
    {
        public static void ProcessSkeletonEnter()
        {
            //if (!SvuConstants.MonitorIsActive)
            //    return;

            switch (SvuConstants.State)
            {
                case ProcessState.AuthenticatingMode:
                case ProcessState.AlarmMode:
                case ProcessState.Authentication_SuccessMode:
                    return;
            }

            // 10 second interval before starting the Authentication timer again
            TimeSpan ts = DateTime.Now - SvuConstants.LastTimeCountDownTimerElapsed;
            if (SvuConstants.LastTimeCountDownTimerElapsed > DateTime.MinValue && ts.TotalSeconds < 30)
                return;

            // Switch to authenticating mode
            SvuConstants.AuthenticationCountDownTimer.Enabled = true;
            SvuConstants.State = ProcessState.AuthenticatingMode;
            FeedbackManager.UpdateStatus();
        }

        public static void SoundAlarm()
        {
            SvuConstants.State = ProcessState.AlarmMode;
            FeedbackManager.UpdateStatus();

            // Play Alarm
            SvuConstants.AlarmPlayer.PlayLooping();

            // Send notification via email and text via 
            TwillioManager.SendText();
        }

        public static void AuthenticationCountDownTimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (SvuConstants.CountDownTimerSecondsElapsed < 10)
            {
                FeedbackManager.AddFeedback(string.Format("Alarm will sound in {0} seconds",
                                                          10 - SvuConstants.CountDownTimerSecondsElapsed));
                SvuConstants.CountDownTimerSecondsElapsed += 1;
            }
            else
            {
                SvuConstants.AuthenticationCountDownTimer.Enabled = false;
                SvuConstants.LastTimeCountDownTimerElapsed = DateTime.Now;
                FeedbackManager.AddFeedback("Alarm Sounded");
                SvuConstants.CountDownTimerSecondsElapsed = 0;

                // Sound Alarm
                SoundAlarm();
            }
        }
    }
}